% Compile with knitr

\documentclass[a4paper]{article}

\usepackage{fullpage}
\usepackage{placeins}
\usepackage{hyperref}

\begin{document}

<<echo = FALSE, message = FALSE, warning = FALSE>>=
source("01_itanes.R")

require(xtable)
@

In this section we want to answer with data based on electoral surveys to two research questions: Over the years,

\begin{enumerate}

\item among right wing voters, are Lega voters significantly more inclined to oppose immigration?

\item based on the opinion expressed by its voters, is Lega Nord the party with the strongest feeling against immigration?

\end{enumerate}

The first Italian electoral survey to include a question on the repondant opinion towards immigration was conducted in 1996. Unfortunately the formulation of questions on immigration were not mantained consistent across time. We then choose this question in each year. For each year we focused on the following survey question:

\begin{description}
\item [1996] Is immigration the most or the second most serious problem in Italy?
\item [2001] Do you consider immigration the most or the second most important problem is Italy?
\item [2006] How much do you agree with the opinion that immigrants are a threat for our culture and our identity?
\item [2008] How much do you agree with the opinion that immigrants are a threat to our culture?
\item [2013] How much trust do you have in immigrants?
\end{description}

The hypothesis that Lega voters are significantly more inclined to oppose immigration (\textbf{research question 1}) is assessed with a chi-squared test for association between two binary variables---vote to Lega and immigration is most or second most important problem---in 1996 and 2001 and a difference of means test (\textit{t}-test) between a categorical variable---vote to Lega---and a continuous variable---intensity of opinion.

The position of Lega voters (\textbf{research question 2}) relatively to the position of voters of other parties is assessed by ranking each party based on the percentage of voters indicating immigration as the most or second most important problem in 1996 and 2001 and based on the average answer on the respective continuous variable in 2006, 2008 and 2013. 

\section{Results}

\subsection{1996}

Among right-wing voters (\Sexpr{names(party_code_96[party_code_96 %in% centreright_parties_96])}):

<<echo = FALSE>>=
round(rq1_1996_tbl*100, 2)
@

with \textit{p}-value \Sexpr{rq1_1996_pvalue}.

\bigskip

Compared to all parties:

<<echo = FALSE, results ='asis'>>=
parties_immigration_96 <- parties_immigration_96[order(parties_immigration_96$Freq, decreasing = TRUE),]
print(xtable(parties_immigration_96), table.placement = '!htb')
write.csv(parties_immigration_96, file = '/Users/francesco/ownCloud/gabriele_lega_immi/output_tables/parties_immigration_96.csv')
@

\FloatBarrier

\subsection{2001}

Among right-wing voters (\Sexpr{names(party_code_01[party_code_01 %in% centreright_parties_01])}):

<<echo = FALSE>>=
round(rq1_2001_tbl*100, 2)
@

with \textit{p}-value \Sexpr{rq1_2001_pvalue}.

\bigskip

Compared to all parties:

<<echo = FALSE, results ='asis'>>=
parties_immigration_01 <- parties_immigration_01[order(parties_immigration_01$Freq, decreasing = TRUE),]
print(xtable(parties_immigration_01), table.placement = '!htb')
write.csv(parties_immigration_01, file = '/Users/francesco/ownCloud/gabriele_lega_immi/output_tables/parties_immigration_01.csv')
@

\FloatBarrier

\subsection{2006}

Among right-wing voters (\Sexpr{names(party_code_06[party_code_06 %in% centreright_parties_06])}):

<<echo = FALSE>>=
rq1_2006_t.test
@

Compared to all parties:

<<echo = FALSE, results ='asis'>>=
parties_immigration_06 <- parties_immigration_06[order(parties_immigration_06$mean_C10_2_scalar, 
                                                       decreasing = TRUE),]
print(xtable(parties_immigration_06), table.placement = '!htb')
write.csv(parties_immigration_06, file = '/Users/francesco/ownCloud/gabriele_lega_immi/output_tables/parties_immigration_06.csv')
@

\FloatBarrier

\subsection{2008}

Among right-wing voters (\Sexpr{names(party_code_08[party_code_08 %in% centreright_parties_08])}):

<<echo = FALSE>>=
rq1_2008_t.test
@

Compared to all parties:

<<echo = FALSE, results ='asis'>>=
parties_immigration_08 <- parties_immigration_08[order(parties_immigration_08$mean_D007_10_scalar, 
                                                       decreasing = TRUE),]
print(xtable(parties_immigration_08), table.placement = '!htb')
write.csv(parties_immigration_08, file = '/Users/francesco/ownCloud/gabriele_lega_immi/output_tables/parties_immigration_08.csv')
@

\FloatBarrier

\subsection{2013}

Among right-wing voters (\Sexpr{names(party_code_13[party_code_13 %in% centreright_parties_13])}):

<<echo = FALSE>>=
rq1_2013_t.test
@

Compared to all parties:

<<echo = FALSE, results ='asis'>>=
parties_immigration_13 <- parties_immigration_13[order(parties_immigration_13$mean_d32_scalar, 
                                                       decreasing = FALSE),]
print(xtable(parties_immigration_13), table.placement = '!htb')
write.csv(parties_immigration_13, file = '/Users/francesco/ownCloud/gabriele_lega_immi/output_tables/parties_immigration_13.csv')
@

\FloatBarrier

<<plot, echo = FALSE, warning = FALSE, message = FALSE, fig.width = 10, fig.cap = "Relative oppostion towards immigration based on voters' opinion">>=

# Mantain only parties with at least 10 respondents

merge_96 <- subset(parties_immigration_96, q133 != 'Altro')
merge_96$immigration_first_or_second <- NULL
merge_01 <- subset(parties_immigration_01, e20 != 'Altro non specificato') 
merge_01$immigration_first_or_second <- NULL
merge_06 <- subset(parties_immigration_06, C140_named != 'Ha votato altra lista') 
merge_08 <- subset(parties_immigration_08) 
merge_13 <- subset(parties_immigration_13)

shapeDF <- function(df, year, desc) {
  
  df$perc <- df$n / sum(df$n)
  
  df <- subset(df, perc > 0.02)
  
  df$n <- NULL
  df$perc <- NULL
  
  names(df) <- c('party', 'value')
  if (desc == TRUE) {
    df$value <- with(df, (value-min(value))/(max(value)-min(value)))
  } else {
    df$value <- with(df, (value-max(value))/(min(value)-max(value)))
  }
  df$party[grepl('lega',df$party, ignore.case = T)] <- "Lega Nord"
  df$party[grepl('rifondazione',df$party, ignore.case = T)] <- "Rifondazione Comunista"
  df$party[grepl('alleanza nazionale',df$party, ignore.case = T)] <- "Alleanza Nazionale"
  df$party[grepl('lista di pietro',df$party, ignore.case = T)] <- "Lista Di Pietro - Italia dei valori"
  df$party[grepl('popolo dell(a|e) libe',df$party, ignore.case = T)] <- "Popolo della Libertá"
  df$party[grepl('casini',df$party, ignore.case = T)] <-  'Unione di Centro'
  df$party <- gsub('(\\s\\(.*?\\))', "", df$party) 
  
  names(df)[2] <- paste0('year.', year)
  return(df)
}

merge_96 <- shapeDF(merge_96, '1996', desc = TRUE)
merge_01 <- shapeDF(merge_01, '2001', desc = TRUE)
merge_06 <- shapeDF(merge_06, '2006', desc = TRUE)
merge_08 <- shapeDF(merge_08, '2008', desc = TRUE)
merge_13 <- shapeDF(merge_13, '2013', desc = FALSE)

slopegraph_df <- merge(merge_96, merge_01, all = TRUE)
slopegraph_df <- merge(slopegraph_df, merge_06, all = TRUE)
slopegraph_df <- merge(slopegraph_df, merge_08, all = TRUE)
slopegraph_df <- merge(slopegraph_df, merge_13, all = TRUE)

require(ggplot2)
require(reshape2)

slopegraph_df <- melt(slopegraph_df, id.vars = 'party',  variable.name = 'year')
slopegraph_df$year <- as.numeric(gsub('year.','',slopegraph_df$year))
slopegraph_df$is_lega <- slopegraph_df$party == 'Lega Nord'

slopegraph_df$party[slopegraph_df$party == "La Margherita - Democrazia e  Liberta  con Rutelli"] <- "La Margherita"

slopegraph_df$party[slopegraph_df$party == "Movimento 5 Stelle Beppegrillo.It"] <- "Movimento 5 Stelle"
  
slopegraph_df$party[slopegraph_df$party == "Scelta Civica Con Monti Per L'italia"] <- "Scelta Civica"

slopegraph_df$party[slopegraph_df$party == "PSI-Partito Socialista"] <- "PSI"

slopegraph_df$party[slopegraph_df$party == "Lista Di Pietro - Italia dei valori"] <- "Italia dei Valori"

slopegraph_df$party[slopegraph_df$party == "Socialisti democratici e Radicali - Rosa nel pugno"] <- "Rosa nel pugno"

require(ggrepel)

slopegraph_df <- subset(slopegraph_df, !is.na(value))

ggplot() + 
  geom_point(data=slopegraph_df, 
            aes(x=year, y=value, label = party), color = 'red') +
  geom_label_repel(data=slopegraph_df, 
            aes(x=year, y=value, label = party, fill = is_lega), 
            alpha = 0.8, fontface = 3) + 
    # geom_text(data=subset(slopegraph_df, party =='Lega Nord'), 
    #         aes(x=year, y=value, label = party), fontface = 2, size = 5) + 
  geom_line(data=slopegraph_df, aes(x=year, y=value, group = party), alpha = 0.2) +
  guides(colour=FALSE) + 
  theme_bw() + labs(y="<- less    oppsition to immigration    more ->") + 
  scale_x_continuous(limits=c(1995,2014),
                     breaks = c(1996,2001,2006,2008,2013)) +
  theme(axis.ticks.y=element_blank(),
        axis.text.y=element_blank()) +
  guides(fill=FALSE)

@


\FloatBarrier

\appendix

\section{Data source}

\url{http://www.itanes.org/}

\end{document}
